<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    img {
      width: 100%;
      height: 100%;
      display: block;
    }

    .box {
      width: 450px;
      margin: 50px;
      display: flex;
      flex-direction: column;

      position: relative;
    }

    .box > .show {
      width: 400px;
      height: 400px;
      border: 1px solid #333;
      position: relative;
    }

    .box > .show > .mask {
      width: 200px;
      height: 200px;
      background-color: yellow;
      opacity: .4;
      position: absolute;
      transform: translate(-50%,-50%);
      top: 0px;
      left: 0px;

      display: none;
    }

    .box > .list {
      width: 100%;
      flex: 1;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      box-sizing: border-box;
      padding: 20px;
    }

    .box > .list > p {
      width: 54px;
      height: 54px;
      border: 1px solid #333;
      margin-right: 10px;
    }

    .box > .list > p.active {
      border-color: red;
    }

    .box > .enlarge {
      width: 400px;
      height: 400px;
      /* background: #000; */
      background-image :url(../images/1.big.jpg);
      position: absolute;
      top: 0;
      left: 101%;
      background-size: 800px 800px;
      background-position: 0 0;
      background-repeat: no-repeat;
      /* display: none; */
    }

  </style>
</head>
<body>

  <div class="box">
    <div class="show">
      <img src="../images/1.jpg" alt="">
      <div class="mask"></div>
    </div>
    <div class="list">
      <p class="active">
        <img
          src="../images/1.small.jpg"
          alt="">
      </p>
      <p>
        <img
          src="../images/2.small.jpg"
          alt="">
      </p>
    </div>
    <div class="enlarge"></div>
  </div>
    <script>
        /* 
        分析需求：1、鼠标移入 遮罩层和 放大图都需要显示，鼠标移除的时候 遮罩层和放大图隐藏
                2、鼠标在移动的时候遮罩层要跟着鼠标动 （鼠标的中间动）（边界问题）
                3、放大图也要按照左边比例来移动
                4、鼠标移到小图上可以切换图片
        */
      let showImgEle = document.querySelector(".show > img");
      let pEles = document.querySelectorAll(".list > p");
      let enlargeImg = document.querySelector(".box > .enlarge"); 
      pEles.forEach((pEle,key)=>{
        pEle.onmouseenter = function (){
          pEles.forEach(item=>item.classList.remove("active"));
          this.classList.add("active");
          showImgEle.src = `../images/${ key + 1 }.jpg` ;
          enlargeImg.style.backgroundImage = `url(../images/${key+1}.big.jpg)`; 
        }
      }); 
      let maskEle = document.querySelector(".mask");
      let showEle = document.querySelector(".show");
      let boxEle = document.querySelector(".box");
    
      
      showEle.onmousemove = function ( event ){
        let showWidth = showEle.offsetWidth ; 
        let showHeight = showEle.offsetHeight ;

        let boxX = boxEle.offsetLeft ; 
        let boxY = boxEle.offsetTop ;

        let maskWidth = maskEle.offsetWidth ; 
        let maskHeight = maskEle.offsetHeight ; 
        
        let minX = boxX ; 
        let maxX = boxX + showWidth ;  
        let minY = boxY ; 
        let maxY = boxY + showHeight ;
        let e = event || window.event ;

        let x = e.clientX ; 
        let y = e.clientY ; 

        if ( x > minX && x < maxX && y > minY && y < maxY ){
          maskEle.style.display = "block" ;
          // maskEle.style.transform = `translate(-50%,-50%)` ;

          // 1
          if ( (x > minX && x < maskWidth/2 + minX) && (y > minY && y < maskHeight/2 + minY) ){
            maskEle.style.left = `${maskWidth/2}px` ;
            maskEle.style.top = `${maskWidth/2}px` ; 
          }else 
          // 2
          if ( ( x < maxX - maskWidth/2 && x > minX + maskWidth/2 ) && (y > minY && y < maskHeight/2 + minY) ){
            maskEle.style.left = `${e.clientX - boxX}px` ;
            maskEle.style.top = `${maskWidth/2}px` ; 
          }  else 
          // 3
          if ( (x > minX && x < maskWidth/2 + minX) && ( y > maskHeight/2 + minY && y < maxY - maskHeight/2 ) ){
            maskEle.style.left = `${maskWidth/2}px` ;
            maskEle.style.top = `${e.clientY - boxY}px` ; 
          } else 
          // 4
          if ( ( x < maxX && x > maxX - maskWidth/2 ) && ( y > maskHeight/2 + minY && y < maxY - maskHeight/2 ) ){
            maskEle.style.left = `${maxX - maskWidth + boxX }px` ;
            maskEle.style.top = `${e.clientY - boxY}px` ; 
          } else 
          // 5
          if ( (x > minX && x < minX + maskWidth/2) && ( y < maxY && y > maxY - maskHeight/2 ) ){
            maskEle.style.left = `${maskWidth/2}px` ;
            maskEle.style.top = `${maxY - maskHeight + boxY }px` ; 
          } else 
          // 6
          if ( ( x < maxX && x > maxX - maskWidth/2 ) && ( y < maxY && y > maxY - maskHeight/2 )){
            maskEle.style.left = `${maxX - maskWidth + boxX }px` ;
            maskEle.style.top = `${maxY - maskHeight + boxY }px` ; 
          } else 
          // 7
          if ( ( x > minX + maskWidth/2 && x < maxX - maskWidth/2 ) && ( y < maxY && y > maxY - maskHeight/2 )){
            maskEle.style.left = `${e.clientX - boxX}px` ;
            maskEle.style.top = `${maxY - maskHeight + boxY }px` ; 
          } else 
          // 8
          if (( x < maxX && x > maxX - maskWidth/2 ) && (y > minY && y < maskHeight/2 + minY) ){
            maskEle.style.left = `${maxX - maskWidth + boxX}px` ;
            maskEle.style.top = `${maskWidth/2}px` ; 
          }
           else {
            maskEle.style.top = `${e.clientY - boxY}px` ; 
            maskEle.style.left = `${e.clientX - boxX}px` ;
          }
          enlargeImg.style.backgroundPosition = `${-(maskEle.offsetLeft - maskWidth/2)*2}px  ${-(maskEle.offsetTop - maskHeight/2)*2}px` ;
        }
      }
    </script>

 
</body>
</html>